"""
学生接口层
"""
from db.models import Student, Course
from lib.utils import prettyprint


def select_school_interface(student_name: str, school_name: str) -> (bool, str):
    """选择学校接口"""
    student_obj = Student.read(student_name)
    # 学生已选择学校
    if student_obj.school_name:
        return False, f'选择失败,当前学生已选择学校[{student_obj.school_name}]'
    # 学生选择学校
    student_obj.school_name = school_name
    student_obj.save()
    return True, f'[{student_name}]选择学校[{school_name}]成功'


def get_school_from_student(student_name: str) -> str:
    """获取学生的学校"""
    return Student.read(student_name).school_name


def select_course_interface(student_name: str, course_name: str) -> (bool, str):
    """选择课程接口"""
    student_obj = Student.read(student_name)
    # 学生已选择此课程
    if course_name in student_obj.score_dict:
        return False, f'选择失败,当前学生已选择课程[{course_name}]'

    # 学生选择课程并初始化分数
    student_obj.score_dict[course_name] = 60
    student_obj.save()
    # 保存学生到课程
    course_obj = Course.read(course_name)
    course_obj.student_list.append(student_obj)
    course_obj.save()

    return True, f'[{student_name}]选择课程[{course_name}]成功'


def check_score_interface(student_name: str) -> (bool, str):
    """查看分数接口"""
    student_obj = Student.read(student_name)
    # 课程为空
    if not student_obj.score_dict:
        return False, f'当前课程为空，请先选择课程！'
    # 打印分数
    data = []
    for course_name in student_obj.score_dict:
        data.append([course_name, student_obj.score_dict[course_name]])
    prettyprint(title=f'[{student_name}]课程分数', fields=['课程名', '分数'], data=data)
    return True, '分数详情见上表'
